#include <iostream>
#include <algorithm>

using namespace std;

string a;
int b;
int gcd(int a , int b)
{
    return b == 0 ? a : gcd(b , a % b);
}
int calc()
{
    long long t = 0;
    for(auto ch : a)
    {
        t = t * 10 + ch - '0';
        t %= b;
    }
    return t;
}
int main()
{
    cin >> a >> b;
    cout << gcd(calc() , b) << endl;
    return 0;
}